package com.edu.robot.common.service.impl;

import cn.hutool.core.util.StrUtil;
import com.edu.robot.model.enums.ResultCodeEnum;
import com.edu.robot.model.error.BizException;
import com.edu.robot.common.service.ChannelManger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-03-30 16:33
 **/

@Slf4j
@Component
public class ChannelMangerImpl implements ChannelManger {

	private static final Map<String, SseEmitter> sseEmitterMap = new ConcurrentHashMap<>();
	/**
	 * 创建连接
	 */
	public SseEmitter createSse(String uid) {
		//默认30秒超时,设置为0L则永不超时
		SseEmitter sseEmitter = new SseEmitter(0L);
		//完成后回调
		sseEmitter.onCompletion(() -> {
			log.info("ChannelMangerImpl createSse [{}]结束连接...................", uid);
			sseEmitterMap.remove(uid);
		});
		//超时回调
		sseEmitter.onTimeout(() -> {
			log.info("ChannelMangerImpl createSse [{}]连接超时...................", uid);
		});
		//异常回调
		sseEmitter.onError(
				throwable -> {
					try {
						log.info(" ChannelMangerImpl createSse [{}]连接异常,{}", uid, throwable.toString());
						sseEmitter.send(SseEmitter.event()
								.id(uid)
								.name("发生异常！")
								.data("发生异常请重试！")
								.reconnectTime(3000));
						sseEmitterMap.put(uid, sseEmitter);
					} catch (IOException e) {
						throw new BizException(ResultCodeEnum.IM_SESSION_CREATE_ERROR);
					}
				}
		);
		try {
			sseEmitter.send(SseEmitter.event().reconnectTime(5000));

		} catch (IOException e) {
			sseEmitter.complete();
			throw new BizException(ResultCodeEnum.IM_SESSION_CREATE_ERROR);
		}
		sseEmitterMap.put(uid, sseEmitter);
		log.info("ChannelMangerImpl createSse [{}]创建sse连接成功！", uid);
		return sseEmitter;
	}

	/**
	 * 给指定用户发送消息
	 *
	 */
	public boolean sendMessage(String uid,String messageId, String message) {
		if (StrUtil.isBlank(message)) {
			log.info(" ChannelMangerImpl sendMessage 参数异常，msg为null,uid:{}", uid);
			return false;
		}
		SseEmitter sseEmitter = sseEmitterMap.get(uid);
		if (sseEmitter == null) {
			log.info("ChannelMangerImpl sendMessage 消息推送失败uid:[{}],没有创建连接，请重试。", uid);
			return false;
		}
		try {
			sseEmitter.send(SseEmitter.event().id(messageId).reconnectTime(5000L).data(message));

			return true;
		}catch (Exception e) {
			log.error("ChannelMangerImpl sendMessage 用户{},消息id:{},{},推送异常:{}", uid,messageId, e.getStackTrace(),e.getLocalizedMessage());
			sseEmitterMap.remove(uid);
			log.info("ChannelMangerImpl sendMessage 用户{},消息id:{},推送异常:{}", uid,messageId, e.getMessage());
			sseEmitter.complete();
			return false;
		}
	}

	/**
	 * 断开
	 * @param uid
	 */
	public void closeSse(String uid){
		if (sseEmitterMap.containsKey(uid)) {
			SseEmitter sseEmitter = sseEmitterMap.get(uid);
			sseEmitter.complete();
			sseEmitterMap.remove(uid);
		}else {
			log.info("ChannelMangerImpl sendMessage 用户{} 连接已关闭",uid);
		}

	}

}
